package com.taotao.portal.service.impl;

import com.taotao.common.HttpUtil;
import com.taotao.common.JsonUtils;
import com.taotao.common.TaotaoResult;
import com.taotao.pojo.TbContent;
import com.taotao.portal.service.TbContentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TbContentServiceImpl implements TbContentService {

    @Value("${rest.url}")
    private String restUrl;

    @Value("${taotao.content.big_ad_category}")
    private Long indexBigAdsCategoryId;

    /**
     * 查询前端页面轮播图
     * @return
     */
    @Override
    public String getIndexBigAds() {
        return getContentByCategoryId(indexBigAdsCategoryId);
    }

    private String getContentByCategoryId(Long categoryId) {
        if (categoryId == null) {
            return "";
        }

        List<Map<String, Object>> result = new ArrayList<>();

        // 发起请求到rest服务，获取到需要的数据，放到这里
        String content = HttpUtil.doGet(restUrl + "/content/all/" + categoryId.longValue());
        List<TbContent> tbContentList = JsonUtils.jsonToList(content, TbContent.class);
        for (TbContent tbContent : tbContentList) {
            Map<String, Object> contentItem = new HashMap<>();
            contentItem.put("srcB", tbContent.getPic2());
            contentItem.put("src", tbContent.getPic());
            contentItem.put("href", tbContent.getUrl());
            contentItem.put("alt", tbContent.getTitle());
            contentItem.put("height", 240);
            contentItem.put("width", 670);
            contentItem.put("heightB", 240);
            contentItem.put("widthB", 550);
            result.add(contentItem);
        }
        return JsonUtils.objectToJson(result);
    }
}
